package com.linghui.videoplus.ipai.activity;

import java.util.HashMap;
import java.util.concurrent.Callable;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

import com.linghui.videoplus.ipai.R;
import com.linghui.videoplus.ipai.VideoPlusApplication;
import com.linghui.videoplus.ipai.api.WSError;
import com.linghui.videoplus.ipai.api.async.Callback;
import com.linghui.videoplus.ipai.api.http.HttpClientAPI;
import com.linghui.videoplus.ipai.api.json.JsonAPI;

/**
 * 启动页
 * 
 * @author chenxf
 * 
 */
public class SplashActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_splash);
		super.doAsync(pCallable, pCallback);

	}

	//0：当前版本是最新, 1：强制更新,  2：选择更新
	public int checkVer(String latestVersionNo,String updateType){
		int caseInt=0;
		String currentVersionNo=getResources().getString(R.string.app_ver);
		currentVersionNo=currentVersionNo.replace(".", "");
		latestVersionNo=latestVersionNo.replace(".", "");
		
		int latestVersionNoInt=Integer.valueOf(latestVersionNo);
		int currentVersionNoInt=Integer.valueOf(currentVersionNo);
		
		if(latestVersionNoInt>currentVersionNoInt){
			if (updateType.equalsIgnoreCase("2")) {
				caseInt=2;
			} else if (updateType.equalsIgnoreCase("1")) {
				caseInt=1;
			}
		}
		return caseInt;
	}
	
	
	//获取版本信息
	Callable<String> pCallable = new Callable<String>() {
		@Override
		public String call() throws Exception {
			String str=null;
			try {
				str = HttpClientAPI.doGet2Api(VideoPlusApplication.getUrlVer(SplashActivity.this), null);
			} catch (WSError e) {
				e.printStackTrace();
			}
			return str;
		};
	};

	// 回调方法
	Callback<String> pCallback = new Callback<String>() {
		@Override
		public void onCallback(String pCallbackValue) {
			if (pCallbackValue != null) {
				JsonAPI jsonApi=new JsonAPI();
				HashMap<String,String> map=jsonApi.analyVersionInfo(pCallbackValue);
				if(map!=null){
					String verStr=map.get("versionStr");
					String upgradeType=map.get("upgradeType");
					String upgradeDescription=map.get("upgradeDescription");
					String downloadUrl=map.get("downloadUrl");
					
					int caseInt=checkVer(verStr,upgradeType);
					switch(caseInt){
					case 0:
						new Handler().postDelayed(skipRunnable,500);
						break;
					case 1:
						VideoPlusApplication.updateApplicationDialog(SplashActivity.this, upgradeDescription, true,downloadUrl);
						break;
					case 2:
						VideoPlusApplication.updateApplicationDialog(SplashActivity.this, upgradeDescription, false,downloadUrl);
						break;
					}
					
				}
			}else{
				new Handler().postDelayed(skipRunnable,500);
			}
		}
	};
	Runnable  skipRunnable= new Runnable(){

		@Override
		public void run() {
			Intent intent = null;
			if (!VideoPlusApplication.checkNetWorkStatus(SplashActivity.this)) {
				showToast(SplashActivity.this.getResources().getString(R.string.network_error));
				VideoCascadeActivity.launch(SplashActivity.this,
						REQUEST_CODE_DEFAULT, REQUEST_CODE_VIDEO_CASCADE);
			} else {
				if (VideoPlusApplication.isExistNickNameFile()) {
					intent = new Intent(SplashActivity.this,
							BrowserActivity.class);
					intent = new Intent(SplashActivity.this,
							BrowserActivity.class);
					intent.putExtra("url", VideoPlusApplication
							.getUrlIndex(SplashActivity.this));
					intent.putExtra(FROM_CODE_KEY, REQUEST_CODE_INIT);
					intent.putExtra(TO_CODE_KEY, REQUEST_CODE_DEFAULT);
				} else {
					intent = new Intent(SplashActivity.this,
							InitActivity.class);
				}
				SplashActivity.this.startActivity(intent);
				overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
				SplashActivity.this.finish();
			}
		}
	};

}
